<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!-- 获取应用上下文路径 -->
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!-- 管理员侧边导航栏 -->
<div class="admin-sidebar bg-dark text-white">
    <div class="sidebar-header p-3">
        <h4><i class="fas fa-cogs me-2"></i>管理后台</h4>
    </div>
    
    <nav class="sidebar-nav">
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="${ctx}/page/admin/users">
                    <i class="fas fa-users me-2"></i>用户管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="${ctx}/page/admin/products">
                    <i class="fas fa-box me-2"></i>商品管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="${ctx}/page/admin/reviews">
                    <i class="fas fa-comments me-2"></i>评价管理
                </a>
            </li>
            <li class="nav-item mt-3">
                <a class="nav-link" href="${ctx}/page/index">
                    <i class="fas fa-home me-2"></i>返回前台
                </a>
            </li>
        </ul>
    </nav>
</div>

<!-- 管理员顶部导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark admin-navbar">
    <div class="container-fluid">
        <span class="navbar-brand mb-0 h1">
            <i class="fas fa-shield-alt me-2"></i>管理员控制台
        </span>
        
        <div class="navbar-nav ms-auto">
            <div class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                    <i class="fas fa-user-shield me-1"></i>
                    <span id="adminUsernameDisplay">管理员</span>
                </a>
                <ul class="dropdown-menu dropdown-menu-end">
                    <li><a class="dropdown-item" href="${ctx}/page/index">返回前台</a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="#" onclick="adminLogout()">退出登录</a></li>
                </ul>
            </div>
        </div>
    </div>
</nav>

<script>
// 管理员用户状态检查
function updateAdminNavbar() {
    apiRequest({
        url: AppConfig.APIS.USER.CURRENT,
        type: 'GET',
        success: function(response) {
            if (response.success && response.data) {
                $('#adminUsernameDisplay').text(response.data.username);
                
                // 检查是否为管理员
                if (response.data.role !== 'ADMIN') {
                    alert('权限不足，请使用管理员账号登录');
                    goToPage(AppConfig.PAGES.LOGIN);
                }
            } else {
                goToPage(AppConfig.PAGES.LOGIN);
            }
        },
        error: function() {
            goToPage(AppConfig.PAGES.LOGIN);
        }
    });
}

// 管理员退出登录
function adminLogout() {
    apiRequest({
        url: AppConfig.APIS.USER.LOGOUT,
        type: 'POST',
        success: function(response) {
            if (response.success) {
                goToPage(AppConfig.PAGES.INDEX);
            }
        }
    });
}

// 页面加载完成后检查管理员状态
$(document).ready(function() {
    updateAdminNavbar();
    
    // 高亮当前页面的导航项
    var currentPath = window.location.pathname;
    $('.sidebar-nav .nav-link').each(function() {
        var linkPath = $(this).attr('href');
        if (linkPath && currentPath.includes(linkPath.split('/').pop())) {
            $(this).addClass('active');
        }
    });
});
</script>

<style>
.admin-sidebar {
    width: 250px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    overflow-y: auto;
}

.admin-navbar {
    margin-left: 250px;
    position: fixed;
    top: 0;
    right: 0;
    z-index: 999;
}

.admin-content {
    margin-left: 250px;
    margin-top: 60px;
    padding: 20px;
}

.sidebar-nav .nav-link {
    color: #adb5bd;
    padding: 12px 20px;
    border-radius: 0;
    transition: all 0.3s;
}

.sidebar-nav .nav-link:hover,
.sidebar-nav .nav-link.active {
    color: #fff;
    background-color: #495057;
}

.sidebar-nav .nav-link i {
    width: 20px;
}
</style>